let timesWS = 0;
App({
    onLaunch: function() {
        let color = wx.getStorageSync('system').color;
        color && this.setNavigationBarColor(color);
        console.log("----(作者邮箱:164897033@qq.com)----");
        this.connectWS();
    },
    setAppSystem: function(o) {
        let t = this;
        t.hyy.request({
            url: "entry/wxapp/System",
            cachetime: "180",
            success: function(e) {
                wx.setStorageSync("system", e.data);
                t.setNavigationBarColor(e.data.color),
                o.data.barTitle ? t.setNavigationBarTitle(o.data.barTitle) : t.setNavigationBarTitle(e.data.pt_name);
                o.setData({
                    System: e.data
                });
                console.log("----加载配置完成----")
            }
        });
        

    },
    globalData: {
        userInfo: null,
        timestamp: 0,
        loading: true,
        localSocket:{}
    },
    ormatDate: function(e) {
        var o = new Date(1e3 * e);
        return o.getFullYear() + "-" + t(o.getMonth() + 1, 2) + "-" + t(o.getDate(), 2) + " " + t(o.getHours(), 2) + ":" + t(o.getMinutes(), 2) + ":" + t(o.getSeconds(), 2);

        function t(e, o) {
            for (var t = "" + e, a = t.length, n = "", s = o; s-- > a;) n += "0";
            return n + t;
        }
    },
    setNavigationBarColor: function(c) {
        wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: c,
        })
    },
    setNavigationBarTitle: function(t) {
        wx.setNavigationBarTitle({
            title: t
        });
    },
    userLogin: function(obj) {
        var _this = this,
            i = obj.data.fxzuid;
        console.log('user_id:', wx.getStorageSync('users').id);
        wx.getStorageSync('users').id || wx.login({
            success: function (t) {
                var code = t.code;
                wx.setStorageSync("code", code),
                wx.getSetting({
                    success: function (t) {
                        if (!t.authSetting['scope.userInfo']) {
                            obj.setData({
                                hydl: !0
                            });
                        }else{
                            wx.getUserInfo({
                                success: function (t) {
                                    wx.setStorageSync("user_info", t.userInfo);
                                    var a = t.userInfo.nickName,
                                        n = t.userInfo.avatarUrl;
                                    _this.hyy.request({
                                        url: "entry/wxapp/Login",
                                        cachetime: "0",
                                        data: {
                                            code: code,
                                            img: n,
                                            name: a
                                        },
                                        success: function (t) {
                                            wx.setStorageSync("key", t.data.session_key),
                                                wx.setStorageSync("openid", t.data.openid);
                                            var e = t.data.openid;
                                            obj.setData({
                                                userinfo: t.data
                                            }),
                                            wx.setStorageSync("users", t.data),
                                            wx.setStorageSync("uniacid", t.data.uniacid),
                                            console.log('登录成功', t.data),
                                            null != i && _this.hyy.request({
                                                url: "entry/wxapp/Binding",
                                                cachetime: "0",
                                                data: {
                                                    fx_user: t.data.id,
                                                    user_id: i
                                                },
                                                success: function (t) { }
                                            });

                                        }
                                    });
                                },
                                fail(f) {
                                    console.log('登录失败', f);
                                }
                            });
                        }
                    }
                });
            }
        })

    },
    saveUserInfo(obj,t, code){
        let _this = this;
        console.log('用户信息', t);
        
    },
    connectWS(){
        let _this = this;
        let socketTask = wx.connectSocket({
            url: 'wss://wss.haiyunyi.cn:8282',
            method: 'GET',
            header: {
                'content-type': 'application/json'
            }
        });
        wx.onSocketOpen(() => {
            clearTimeout(timesWS), timesWS = 0;
            console.log('Socket连接成功!');
            _this.globalData.socketTask = socketTask;
        });
        // 监听关闭事件自动重连
        wx.onSocketClose(function (res) {
            _this.repconnectWS();
        });
        wx.onSocketError(function (res) {
            console.log('socket连接错误', res);
        });
    },
    repconnectWS(){
        let _this = this;
        console.log('socke连接失败,10秒后自动重连...');
        timesWS = setTimeout(function () {
            _this.connectWS();
        }, 1000*10);
        
    },
    hyy: require("hyy_city/resource/js/util.js"),
    siteInfo: require("siteinfo.js")

})